package com.anhry.app.safety.manage.system.struts.inteceptor;

import java.lang.reflect.Method;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.anhry.app.safety.manage.system.bean.TbActionLog;
import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.util.DateFormater;
import com.anhry.app.safety.util.Util;
import com.anhry.app.safety.util.bean.Actionlog;
import com.anhry.app.safety.util.service.UtilService;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.Interceptor;
import java.util.Date;

/**
 * @date:2012-10-25
 * @remark：记录重要操作日志
 * @author:dongbin
 */
public class ActionLogInterceptor implements Interceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Resource
	private UtilService utilService;

	public UtilService getUtilService() {
		return utilService;
	}

	public void setUtilService(UtilService utilService) {
		this.utilService = utilService;
	}

	public void destroy() {
		
	}

	public void init() {
		
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		ActionProxy proxy = invocation.getProxy();
		String methodName = proxy.getMethod();
		if (methodName == null) {
			methodName = "execute";
		}
		Method method = invocation.getAction().getClass().getMethod(methodName,new Class[0]);
		if ((method != null) && (method.isAnnotationPresent(Actionlog.class))) {
			HttpServletRequest request = ServletActionContext.getRequest();
			Actionlog actionlog = (Actionlog) method.getAnnotation(Actionlog.class);
			TbUser user = (TbUser)request.getSession().getAttribute(Util.SESSION_ADMINUSER);
			TbActionLog obj = new TbActionLog();
			obj.setActionDate(new Date());
			obj.setActionInfo(DateFormater.formatDateTime(new Date())+actionlog.logmsg());
			obj.setIsDele("0");
			obj.setUserId(user.getId());
			obj.setUserName(user.getUserName());
			this.utilService.save(obj);
		}
		return invocation.invoke();
	}

}
